﻿using System;

class BiggerThanTwoNeighbors
{
    //Exercise 5.
    //Write a method that checks if the element at given position in given array of integers is bigger than its two neighbors 
    //(when such exist).
    //Sample:  int[] array = { 4, 5, 1, 4, 2, 5, 8 };  

    static void Main()
    {
        //enter the length and the elements of the array from the console
        Console.Write("Type the length of the array:  ");
        int length = int.Parse(Console.ReadLine());
        Console.WriteLine("Type the elements of the array:");
        int[] array = new int[length];
        for (int i = 0; i < length; i++)
        {
            array[i] = int.Parse(Console.ReadLine());
        }              

        Console.Write("Type the position of the element to be compared to its two neighbours: ");
        int position = int.Parse(Console.ReadLine());
        
        if (position == 0 || position == array.Length - 1)
        {
            FirstLastPosition(array, position);
        }
        else if (position < 0 || position > array.Length - 1)
        {
            Console.WriteLine("Invalid position!");
            return;
        }
        else
        {
            CompareIntegers(array, position);
        }
    }


    private static void CompareIntegers(int[] array, int position)
    {
        if (array[position] > array[position + 1] && array[position] > array[position - 1])
        {
            Console.WriteLine("The element at position {0} is bigger than its two neighbours", position);
        }
        else if (array[position] > array[position + 1])
        {
            Console.WriteLine("The element at position {0} is bigger only than its right-hand neighbour", position);
        }
        else if (array[position] > array[position - 1])
        {
            Console.WriteLine("The element at position {0} is bigger only than its left-hand neighbour", position);
        }
        else
        {
            Console.WriteLine("The element at position {0} is NOT bigger than its two neighbours", position);
        }
    }

    private static void FirstLastPosition(int[] array, int position)
    {
        if (position == 0 && (array[position] > array[position+ 1]))
        {
            Console.WriteLine("The element at position {0} is bigger than its only neighbour", position);
        }
        else if (position == array.Length - 1 && array[position] > array[array.Length - 2])
        {
            Console.WriteLine("The element at position {0} is bigger than its only neighbour", position);
        }
        else
        {
            Console.WriteLine("The element at position {0} is NOT bigger than its only neighbour", position);
        }
    }

}

